// pages/self/self.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showWalletConfirm: false,
    showConfirm: false,
    last_login_phones: [],
    userInfo: {

    },
    showImage: '',
  },
  checkLogin() {
    wx.login({
      success: (wxres) => {
        let data = {
          code: wxres.code,
          token: ''
        }
        wx.request({
          url: 'https://adminii.sengain.top/fishApi/user/wxlogin',
          method: 'get',
          data: data,
          dataType: 'json',
          success: (res) => {
            let userInfo = res.data.data.userInfo
            wx.setStorageSync('token', userInfo.token)
            if (userInfo.last_login_phones.length > 0) {
              this.setData({
                showConfirm: true,
                last_login_phones: userInfo.last_login_phones
              })
            } else {
              this.toLoginPage()
            }
          }
        })
      },
    })
  },
  // 手机号登录
  toPhoneLogin(e) {
    let data = {
      token: wx.getStorageSync('token'),
      mobile_phone: e.currentTarget.dataset.name
    }
    app.request('get', '/user/login', data, res => {
      console.log(res);
      wx.setStorageSync('userInfo', res.data.data.userInfo)
      // console.log(wx.getStorageSync('userInfo'));
      this.setData({
        showConfirm: false,
        userInfo: res.data.data.userInfo,
        showImage: res.data.data.userInfo.avatar
      })

    })
  },
  toLoginPage() {
    this.setData({
      showConfirm: false
    })
    wx.navigateTo({
      url: `/pages/login/login`
    });
  },
  toGoUrl(e) {

    if (this.data.userInfo.mobile_phone) {
      if (e.currentTarget.dataset.url == '/pages/realNameAuthentication/realNameAuthentication') {
        this.setData({
          showWalletConfirm: true
        })
      } else {
        wx.navigateTo({
          url: `${e.currentTarget.dataset.url}`
        });
      }
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'error',
        duration: 1000
      })
    }




  },

  toRealName() {
    this.setData({
      showWalletConfirm: false
    })
    wx.navigateTo({
      url: `/pages/realNameAuthentication/realNameAuthentication`
    });
  },

  toUserInfo(e) {
    if (this.data.userInfo.mobile_phone) {
      let info = this.data.userInfo
      wx.navigateTo({
        url: `/pages/userInfo/userInfo?mobile_phone=${info.mobile_phone}`
      });
    } else {
      this.checkLogin()
    }
  },
  tellphone() {
    wx.makePhoneCall({
      phoneNumber: '13084061350'
    })
  },

  getUserInfo(mobile_phone) {
    let data = {
      token: wx.getStorageSync('token'),
      mobile_phone: mobile_phone
    }
    let _this = this
    app.request('get', '/user/getUserInfo', data, res => {
      let userInfo = res.data.data
      _this.setData({
        userInfo: userInfo,
        showImage: userInfo.avatar
      })
    })
  },

  toMyTickets(e) {
    if (this.data.userInfo.mobile_phone) {
      wx.navigateTo({
        url: `/pages/myTickets/myTickets?id=${e.currentTarget.dataset.name}`
      });
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'error',
        duration: 1000
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2 //这个数字是当前页面在tabBar中list数组的索引
      })
    }
    let info = wx.getStorageSync('userInfo')
    let showImage = ''
    if (info.avatar) {
      showImage = info.avatar
    }
    this.setData({
      userInfo: info,
      showImage: ''
    })
    if (info.mobile_phone) {
      this.getUserInfo(info.mobile_phone)
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})